#!/bin/bash

set -e

# Function to validate version number format
validate_version() {
echo "I should have used dpkg to check but i'm too lazy so you should check yourselves input"
}
# Parse arguments
for i in "$@"; do
    case $i in
        --new-pkg-name=*)
        NEW_PKG_NAME="${i#*=}"
        shift
        ;;
        --orig-pkg-name=*)
        ORIG_PKG_NAME="${i#*=}"
        shift
        ;;
        --version=*)
        VERSION="${i#*=}"
        validate_version $VERSION || exit 1
        shift
        ;;
        *)
echo "Usage: $0 [--new-pkg-name=NAME --orig-pkg-name=NAME --version=VERSION]"
exit 1
        ;;
    esac
done

# Check argument count and types
if [ "$#" -gt 0 ] && { [ -n "$NEW_PKG_NAME" ] || [ -n "$ORIG_PKG_NAME" ] || [ -n "$VERSION" ]; }; then
    echo "Error: Mixed argument types."
    exit 1
fi







if [ "$(which wget)" = "" ]; then
    echo "Nope, you need wget first"
    exit
fi

if [ "$(which loongnix-run)" = "" ]; then
    if [ "$(which spark-store)" = "" ]; then
        echo "Nope, you need spark-store first"
        exit
    fi
    xdg-open spk://store/tools/amber-ce-loongnix
    echo "Nope, you need loongnix-run first"
    exit
fi

IS_ACE_ENV=1 loongnix-run echo "Welcome to ACE-Loongnix! Trying to create integrate package"
if [ -z "$(loongnix-run which aptss)" ]; then
    wget https://amber-ce-resource.spark-app.store/store/depends/spark-store-console-in-container_latest_all.deb -O /tmp/ssconsole.deb
    IS_ACE_ENV=1 sudo loongnix-run apt install /tmp/ssconsole.deb -y
    rm /tmp/ssconsole.deb -f
fi

# Step 1: Get the version number and other details of the original package
sudo loongnix-run aptss update
sudo loongnix-run aptss install spark-store-console-in-container --only-upgrade -y
ORIG_PKG_DETAILS=$(loongnix-run aptss show $ORIG_PKG_NAME)
# Only retrieve version number if not provided by argument
if [ -z "$VERSION" ]; then
VERSION=$(echo "$ORIG_PKG_DETAILS" | grep Version | cut -d ' ' -f 2)
fi
MAINTAINER=$(echo "$ORIG_PKG_DETAILS" | grep Maintainer | cut -d ':' -f 2 | sed 's/^\s*//')
DESCRIPTION=$(echo "$ORIG_PKG_DETAILS" | grep Description | cut -d ':' -f 2- | sed 's/^\s*//')
HOMEPAGE="https://gitee.com/spark-building-service/ace-host-package-creator"

# Check if the version number was retrieved successfully
if [ -z "$VERSION" ]; then
    echo "Error: Unable to retrieve the version number for the package $ORIG_PKG_NAME."
    exit 1
fi

# Step 2: Create the directory structure for the new package
PKG_DIR="$(mktemp -d)"
DEBIAN_DIR="${PKG_DIR}/DEBIAN"
mkdir -p "$DEBIAN_DIR"

# Step 3: Create and modify the control file with the details from the original package
cat > "${DEBIAN_DIR}/control" << EOF
Package: $NEW_PKG_NAME
Version: $VERSION
Maintainer: $MAINTAINER
Priority: optional
Section: utils
Installed-Size: 10
Depends: amber-ce-loongnix,bash,wget,sudo,libnotify-bin
Description: $DESCRIPTION
Architecture: all
Homepage: $HOMEPAGE
EOF


# Step 4: Create and write the postinst and postrm scripts
echo "#!/bin/bash " > "${DEBIAN_DIR}/postinst"
echo "REAL_PACKAGE=$ORIG_PKG_NAME" >> "${DEBIAN_DIR}/postinst"
cat >> "${DEBIAN_DIR}/postinst" << 'EOF'
function notify-send(){
 #Detect the user using such display
    local user=$(who | awk '{print $1}' | head -n 1)

    #Detect the id of the user
    local uid=$(id -u $user)
sudo -u $user DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$@"
}

function zenity(){
 #Detect the user using such display
    local user=$(who | awk '{print $1}' | head -n 1)

    #Detect the id of the user
    local uid=$(id -u $user)
sudo -u $user DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus zenity "$@"
}

###检查aptss在不在
if [ -z `which loongnix-run` ];then
echo "Oh no, ssinstall have not installed loongnix-run dependency yet! Exit"
exit 1
fi
IS_ACE_ENV=1 loongnix-run echo "Welcome to BCM! Trying to install package"
if [ -z "`loongnix-run which aptss`" ];then
wget https://amber-ce-resource.spark-app.store/store/depends/spark-store-console-in-container_latest_all.deb -O /tmp/ssconsole.deb
IS_ACE_ENV=1 loongnix-run apt update
IS_ACE_ENV=1 loongnix-run apt install /tmp/ssconsole.deb -y
rm /tmp/ssconsole.deb -f
fi

notify-send "Installing $REAL_PACKAGE ACE, may cost some time please ensure your network is fine" 
notify-send "正在ACE兼容环境中安装$REAL_PACKAGE，可能会花费一些时间，请保持网络畅通" 
IS_ACE_ENV=1 loongnix-run aptss update
IS_ACE_ENV=1 loongnix-run  aptss install spark-store-console-in-container --only-upgrade -y
IS_ACE_ENV=1 loongnix-run aptss install $REAL_PACKAGE -y

IS_ACE_ENV=1 loongnix-run aptss clean

EOF

echo "#!/bin/bash" > "${DEBIAN_DIR}/postrm"
echo "REAL_PACKAGE=$ORIG_PKG_NAME" >> "${DEBIAN_DIR}/postrm"
cat >> "${DEBIAN_DIR}/postrm" << 'EOF'


loongnix-run echo "Welcome to ACE! Trying to remove package"

if [ "$1" = "remove" ] || [ "$1" = "purge" ];then

loongnix-run apt autopurge $REAL_PACKAGE -y
loongnix-run apt clean
else
echo "非卸载，跳过卸载"
fi
true
EOF


chmod +x "${DEBIAN_DIR}/postinst"
chmod +x "${DEBIAN_DIR}/postrm"

# Step 5: Build the new .deb package
dpkg-deb --build $PKG_DIR "$(pwd)"
rm -rf $PKG_DIR
